<template>
	<div>
		<!--引用表格-->
		<BasicTable @register="registerTable" :rowSelection="rowSelection" :searchInfo="searchInfo">
			<!--插槽:table标题-->
			<template #tableTitle>
				<!-- 	<a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增</a-button>
				<a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button>
				<j-upload-button type="primary" preIcon="ant-design:import-outlined"
					@click="onImportXls">导入</j-upload-button> -->


				<a-button type="primary" preIcon="ant-design:export-outlined" @click="openLine"
					v-if="selectedRowKeys.length == 1"> 查看线路</a-button>
				<!-- <a-dropdown v-if="selectedRowKeys.length > 0">
					<template #overlay>
						<a-menu>
							<a-menu-item key="1" @click="batchHandleDelete">
								<Icon icon="ant-design:delete-outlined"></Icon>
								删除
							</a-menu-item>
						</a-menu>
					</template>
					<a-button>批量操作
						<Icon icon="mdi:chevron-down"></Icon>
					</a-button>
				</a-dropdown> -->
			</template>
			<!--操作栏-->
			<template #action="{ record }">
				<TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
			</template>
			<!--字段回显插槽-->
			<template v-slot:bodyCell="{ column, record, index, text }">
			</template>

		</BasicTable>
		<!-- 表单区域 -->
		<BsTravelgroupMaintuortravellinerecordModal @register="registerModal" @success="handleSuccess">
		</BsTravelgroupMaintuortravellinerecordModal>
		<tourRouteSelectModal @register="registerLineModal" @success="handleLineSuccess">
		</tourRouteSelectModal>
	</div>
</template>

<script lang="ts" name="journey-bsTravelgroupMaintuortravellinerecord" setup>
	import { ref, computed, unref, inject, watch } from 'vue';
	import { BasicTable, useTable, TableAction } from '/@/components/Table';
	import { useModal } from '/@/components/Modal';
	import { useListPage } from '/@/hooks/system/useListPage'
	import BsTravelgroupMaintuortravellinerecordModal from './components/BsTravelgroupMaintuortravellinerecordModal.vue'
	import tourRouteSelectModal from '../../../module/tourRouteSelectModal.vue'
	import { columns, searchFormSchema } from './BsTravelgroupMaintuortravellinerecord.data';
	import { getListByMainId, deleteOne, batchDelete, getImportUrl, getExportUrl, closeOrOpenApi } from './BsTravelgroupMaintuortravellinerecord.api';
	import { downloadFile } from '/@/utils/common/renderUtils';
	import { useUserStore } from '/@/store/modules/user';
	import { formatTimestamp } from '/@/utils/common/renderUtils';
	import {isEmpty} from "/@/utils/is";
	import { useMessage } from '/@/hooks/web/useMessage';
	const checkedKeys = ref<Array<string | number>>([]);
	const userStore = useUserStore();
	const mainId = inject('mainId') || '';
	const $message = useMessage()
	const searchInfo = {};
	//注册model
	const [registerModal, { openModal }] = useModal();
	const [registerLineModal, { openModal:openLineModal }] = useModal();
	//注册table数据
	const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
		tableProps: {
			title: '客户旅游路线主记录',
			api: getListByMainId,
			columns,
			canResize: false,
			useSearchForm: false,
			actionColumn: {
				width: 180,
			},
			isTreeTable: false,
			pagination: {
				current: 1,
				pageSize: 5,
				pageSizeOptions: ['5', '10', '20'],
			},
			actionColumn: {
				width: 160,
				fixed: 'right'
			},
		},
		exportConfig: {
			name: "客户旅游路线主记录",
			url: getExportUrl,
		},
		importConfig: {
			url: getImportUrl,
			success: handleSuccess
		},
	})

	const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext
	watch(mainId, () => {
		searchInfo['mainId'] = unref(mainId);
		reload();
	}
	);
	function openLine() {
	 	openLineModal(true,{
			pretravellineid:rowSelection.selectedRows[0].pretravellineid
		}) 
	}

	/**
	 * 新增事件
	 */
	function handleAdd() {
		console.log(mainId)
		if (isEmpty(unref(mainId))) {
			$message.createMessage.warning('请选择一个主表信息')
			return;
		}
		openModal(true, {
			isUpdate: false,
			showFooter: true,
		});
	}
	/**
	 * 编辑事件
	 */
	function handleEdit(record : Recordable) {
		record.startdateStr=formatTimestamp(record.startdate)
		record.expiredateStr=formatTimestamp(record.expiredate)
		openModal(true, {
			record,
			isUpdate: true,
			showFooter: true,
		});
	}
	/**
	 * 详情
	*/
	function handleDetail(record : Recordable) {
		openModal(true, {
			record,
			isUpdate: true,
			showFooter: false,
		});
	}
	async function closeOrOpen(record : Recordable, i) {
		//请求

		let stats = "Wait"
		if (i == 3) {
			stats = "Cancel"
		}
		await closeOrOpenApi({ id: record.id, stats: stats }, reload())

	}
	/**
	 * 删除事件
	 */
	async function handleDelete(record) {
		await deleteOne({ id: record.id }, handleSuccess);
	}
	/**
	 * 批量删除事件
	 */
	async function batchHandleDelete() {
		await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
	}
	/**
	 * 成功回调
	 */
	function handleSuccess() {
		(selectedRowKeys.value = []) && reload();
	}
	/**
	   * 操作栏
	   */
	function getTableAction(record) {
		return [
			{
				label: '编辑',
				onClick: handleEdit.bind(null, record, 1),
			},
			{
				label: '关闭',
				popConfirm: {
					title: '是否确认关闭本行程报名',
					confirm: closeOrOpen.bind(null, record, 3),
					placement: 'topLeft',
				},
				ifShow: (_action) => {
					return record.stats == 'Wait'; // 根据业务控制是否显示: 非enable状态的不显示启用按钮
				}
			}, {
				label: '开启',
				popConfirm: {
					title: '是否确认开启本行程报名',
					confirm: closeOrOpen.bind(null, record),
					placement: 'topLeft',
				},
				ifShow: (_action) => {
					return record.stats == 'Cancel'; // 根据业务控制是否显示: 非enable状态的不显示启用按钮
				}
			},
		]
	}
	/**
	   * 下拉操作栏
	   */
	function getDropDownAction(record) {
		return [
			{
				label: '详情',
				onClick: handleDetail.bind(null, record, 2),
			},
			{
				label: '删除',
				popConfirm: {
					title: '是否确认删除',
					confirm: handleDelete.bind(null, record),
					placement: 'topLeft',
				},
			},
		]
	}
</script>

<style scoped>

</style>